package senior.ioExer2;

import java.io.*;

/**
 * 一个模板文件wuliu. template的内容如下：
 * 您的运单号为{danhao}的快件已到达{weizhi},请根据提取码{tiquma}提取，感谢您的支持
 * <p>
 * 请把上面{}内的内容（包括{})替换为具体的信息，将替换后的内容写入到 《物流.txt》中
 *
 * @author shkstart
 * @creat 2022-04-22:03
 */
public class Exer10 {
    public static void main(String[] args) {
        File file = new File("wuliu.template");
        File file1 = new File("物流.txt");
        FileReader fr = null;
        FileWriter fw = null;

        try {
            fr = new FileReader(file);
            fw = new FileWriter(file1);

            char[] chars = new char[20];
            StringBuilder str = new StringBuilder();
            int l;
            while ((l = fr.read(chars)) != -1) {
                str.append(new String(chars, 0, l));
            }
            String string = str.toString();
            //String zhengze="[a-z]+";
            string = string.replaceAll("\\{danhao\\}", "12345");
            string = string.replaceAll("\\{weizhi\\}", "清华物流园");
            string = string.replaceAll("\\{tiquma\\}", "04gH");
            System.out.println(string);
            fw.write(string);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fw != null) {
                    fw.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
